package NewCoder;

import java.util.Stack;

public class BM44 {
    public boolean isValid (String s) {
        // write code here

        if(s.length()==0) return true;
        char[] ch=s.toCharArray();
        Stack<Character> stack=new Stack<>();
        for(int i=0;i<ch.length;i++){
            if(ch[i]=='(' || ch[i]=='[' || ch[i]=='{'){
                stack.push(ch[i]);
            }else{
                if(stack.isEmpty()){
                    return false;
                }else if(ch[i]==')'){
                    if(stack.pop()!='(') return false;
                }else if(ch[i]==']'){
                    if(stack.pop()!='[') return false;
                }else if(ch[i]=='}'){
                    if(stack.pop()!='{') return false;
                }
            }
        }
        return stack.isEmpty()?true:false;
    }
}
